package com.dianping.cat.configuration.web.url.transform;

import com.dianping.cat.configuration.web.url.IEntity;
import com.dianping.cat.configuration.web.url.IVisitor;
import com.dianping.cat.configuration.web.url.entity.Code;
import com.dianping.cat.configuration.web.url.entity.PatternItem;
import com.dianping.cat.configuration.web.url.entity.UrlPattern;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;














public class DefaultJsonBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;
  
/*  33 */   public DefaultJsonBuilder() { this(false); }



/*  37 */   public DefaultJsonBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  
  public DefaultJsonBuilder(boolean compact, StringBuilder sb) {
/*  41 */     this.m_compact = compact;
/*  42 */     this.m_sb = sb;
/*  43 */     this.m_visitor = this;
  }
  
  protected void arrayBegin(String name) {
/*  47 */     indent();
/*  48 */     this.m_sb.append('"').append(name).append(this.m_compact ? "\":[" : "\": [\r\n");
/*  49 */     this.m_level++;
  }
  
  protected void arrayEnd(String name) {
/*  53 */     this.m_level--;

/*  55 */     trimComma();
/*  56 */     indent();
/*  57 */     this.m_sb.append("],").append(this.m_compact ? "" : "\r\n");
  }
  
  protected void attributes(Map<String, String> dynamicAttributes, Object... nameValues) {
/*  61 */     int len = nameValues.length;

/*  63 */     for (int i = 0; i + 1 < len; i += 2) {
/*  64 */       Object attrName = nameValues[i];
/*  65 */       Object attrValue = nameValues[i + 1];

/*  67 */       if (attrValue != null) {
/*  68 */         if (attrValue instanceof Collection) {

/*  70 */           Collection<Object> items = (Collection)attrValue;

/*  72 */           if (!items.isEmpty()) {
/*  73 */             indent();
/*  74 */             this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

/*  76 */             for (Object item : items) {
/*  77 */               this.m_sb.append(' ');
              toString(this.m_sb, item);
/*  79 */               this.m_sb.append(',');
            }
            
/*  82 */             this.m_sb.setLength(this.m_sb.length() - 1);
/*  83 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }
/*  85 */         } else if (attrValue.getClass().isArray()) {
/*  86 */           int length = Array.getLength(attrValue);

/*  88 */           if (length > 0) {
/*  89 */             indent();
/*  90 */             this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

/*  92 */             for (int j = 0; j < length; j++) {
/*  93 */               Object item = Array.get(attrValue, j);
/*  94 */               this.m_sb.append(' ');
/*  95 */               toString(this.m_sb, item);
/*  96 */               this.m_sb.append(',');
            }
            
/*  99 */             this.m_sb.setLength(this.m_sb.length() - 1);
/* 100 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }
        
/* 103 */         } else if (this.m_compact) {
/* 104 */           this.m_sb.append('"').append(attrName).append("\":");
/* 105 */           toString(this.m_sb, attrValue);
/* 106 */           this.m_sb.append(",");
        } else {
/* 108 */           indent();
/* 109 */           this.m_sb.append('"').append(attrName).append("\": ");
/* 110 */           toString(this.m_sb, attrValue);
/* 111 */           this.m_sb.append(",\r\n");
        }
      }
    }

    
/* 117 */     if (dynamicAttributes != null) {
/* 118 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 119 */         if (this.m_compact) {
/* 120 */           this.m_sb.append('"').append((String)e.getKey()).append("\":");
/* 121 */           toString(this.m_sb, e.getValue());
/* 122 */           this.m_sb.append(","); continue;
        }
/* 124 */         indent();
/* 125 */         this.m_sb.append('"').append((String)e.getKey()).append("\": ");
/* 126 */         toString(this.m_sb, e.getValue());
/* 127 */         this.m_sb.append(",\r\n");
      }
    }
  }

  
  public String build(IEntity<?> entity) {
/* 134 */     objectBegin(null);
/* 135 */     entity.accept(this);
/* 136 */     objectEnd(null);
/* 137 */     trimComma();

/* 139 */     return this.m_sb.toString();
  }
  
  public String buildArray(Collection<? extends IEntity<?>> entities) {
/* 143 */     this.m_sb.append('[');

/* 145 */     if (entities != null) {
/* 146 */       for (IEntity<?> entity : entities) {
/* 147 */         objectBegin(null);
/* 148 */         entity.accept(this);
/* 149 */         objectEnd(null);
      }
      
/* 152 */       trimComma();
    }
    
/* 155 */     this.m_sb.append(']');

/* 157 */     return this.m_sb.toString();
  }
  
  protected void indent() {
/* 161 */     if (!this.m_compact) {
/* 162 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 163 */         this.m_sb.append("   ");
      }
    }
  }
  
  protected void objectBegin(String name) {
/* 169 */     indent();

/* 171 */     if (name == null) {
/* 172 */       this.m_sb.append("{").append(this.m_compact ? "" : "\r\n");
    } else {
/* 174 */       this.m_sb.append('"').append(name).append(this.m_compact ? "\":{" : "\": {\r\n");
    }
    
/* 177 */     this.m_level++;
  }
  
  protected void objectEnd(String name) {
/* 181 */     this.m_level--;

/* 183 */     trimComma();
/* 184 */     indent();
/* 185 */     this.m_sb.append(this.m_compact ? "}," : "},\r\n");
  }
  
  protected void toString(StringBuilder sb, Object value) {
/* 189 */     if (value == null) {
/* 190 */       sb.append("null");
/* 191 */     } else if (value instanceof Boolean || value instanceof Number) {
/* 192 */       sb.append(value);
    } else {
/* 194 */       String val = value.toString();
/* 195 */       int len = val.length();

/* 197 */       sb.append('"');

/* 199 */       for (int i = 0; i < len; i++) {
/* 200 */         char ch = val.charAt(i);

/* 202 */         switch (ch) {
          case '"':
          case '/':
          case '\\':
/* 206 */             sb.append('\\').append(ch);
            break;
          case '\t':
/* 209 */             sb.append("\\t");
            break;
          case '\r':
/* 212 */             sb.append("\\r");
            break;
          case '\n':
/* 215 */             sb.append("\\n");
            break;
          default:
/* 218 */             sb.append(ch);
            break;
        }
      
      }
/* 223 */       sb.append('"');
    }
  }
  
  protected void trimComma() {
/* 228 */     int len = this.m_sb.length();

/* 230 */     if (this.m_compact) {
/* 231 */       if (len > 1 && this.m_sb.charAt(len - 1) == ',') {
/* 232 */         this.m_sb.replace(len - 1, len, "");
      }
    }
/* 235 */     else if (len > 3 && this.m_sb.charAt(len - 3) == ',') {
/* 236 */       this.m_sb.replace(len - 3, len - 2, "");
    }
  }



  
/* 243 */   public void visitCode(Code code) { attributes(null, new Object[] { "id", code.getId(), "name", code.getName(), "status", code.getStatus() }); }




/* 248 */   public void visitPatternItem(PatternItem patternItem) { attributes(null, new Object[] { "group", patternItem.getGroup(), "name", patternItem.getName(), "pattern", patternItem.getPattern(), "domain", patternItem.getDomain(), "id", Integer.valueOf(patternItem.getId()) }); }



  
  public void visitUrlPattern(UrlPattern urlPattern) {
/* 254 */     if (!urlPattern.getPatternItems().isEmpty()) {
/* 255 */       objectBegin("pattern-items");

/* 257 */       for (Map.Entry<String, PatternItem> e : urlPattern.getPatternItems().entrySet()) {
/* 258 */         String key = String.valueOf(e.getKey());

/* 260 */         objectBegin(key);
/* 261 */         ((PatternItem)e.getValue()).accept(this.m_visitor);
/* 262 */         objectEnd(key);
      }
      
/* 265 */       objectEnd("pattern-items");
    }
    
/* 268 */     if (!urlPattern.getCodes().isEmpty()) {
/* 269 */       objectBegin("codes");

/* 271 */       for (Map.Entry<Integer, Code> e : urlPattern.getCodes().entrySet()) {
/* 272 */         String key = String.valueOf(e.getKey());

/* 274 */         objectBegin(key);
/* 275 */         ((Code)e.getValue()).accept(this.m_visitor);
/* 276 */         objectEnd(key);
      }
      
/* 279 */       objectEnd("codes");
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-core-3.0.0.jar!/com/dianping/cat/configuration/web/url/transform/DefaultJsonBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */